<?php

namespace app\tenant\controller;

use think\Controller;
use Util\SysDb;
use think\Db;

class Ad extends Controller
{
    public function Adlist()
    {
        $status = trim(input('param.status'));
        $type = trim(input('param.type'));
        $where=array(
            "status" => $status,
            "type" => $type
        );
        foreach ($where as $key=>$value)
        {
            if(empty($value)){
                unset($where[$key]);
            }
        }
        $page = trim(input('param.pageNo'));
        $limit = trim(input('param.pageSize'));
        $tenantid = trim(input('param.tenantid'));
        $tol = (intval($page) - 1) * $limit;
        $list = DB::name("tenant_advert")->order("create_time desc")->where($where)->where(['tenant_id' =>  $tenantid])->limit($tol, $limit)->select();
        $this->db = new SysDb();
        $count = $this->db->table('tenant_advert')->where($where)->where(['tenant_id' =>  $tenantid])->count();
        $data = array();
        array_push($data, $list);
        array_push($data, $count);
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $data]));
    }

    public function Addel()
    {
        $id = trim(input('param.id'));
        $admin = Db::name('tenant_advert')->where('id', $id)->delete();;
        if ($admin) {
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "删除成功"]));
        }
    }

    public function Adupadteadd()
    {
        $id = trim(input('param.id'));
        $sort = trim(input('param.sort'));
        $status = trim(input('param.status'));
        $type = trim(input('param.type'));
        $remark = trim(input('param.remark'));
        $url = trim(input('param.url'));
        $img = trim(input('param.image'));
        $needle = 'base64'; //判断是否包含a这个字符
        $tmparray = explode($needle, $img);
        if (count($tmparray) > 1) {
            $imageName = "25220_" . date("His", time()) . "_" . rand(1111, 9999) . '.png';
            if (strstr($img, ",")) {
                $image = explode(',', $img);
                $image = $image[1];
            }
            $path = "./uploads/" . date("Ymd", time());
            $paths = "http://127.0.0.1:8000/uploads/" . date("Ymd", time());
            if (!is_dir($path)) { //判断目录是否存在 不存在就创建
                mkdir($path, 0777, true);
            }
            $imageSrc = $path . "/" . $imageName; //图片名字
            $r = file_put_contents($imageSrc, base64_decode($image)); //返回的是字节数
            $imageSrcs = $paths . "/" . $imageName; //图片名字
            if (!$r) {
                exit(json_encode(['message' => 'error', 'result' => "失败"]));
            } else {
                $imgs = $imageSrcs;
            }
        } else {
            $imgs = $img;
        }
        if ($id == "kk") {
            $data['sort'] = $sort;
            $data['status'] = $status;
            $data['type'] = $type;
            $data['remark'] = $remark;
            $data['url'] = $url;
            $data['image'] = $imgs;
            $data['tenant_id'] =trim(input('param.tenantid'));
            $this->db = new SysDb();

            $count = $this->db->table('tenant_advert')->insert($data);
            if ($count) {
                exit(json_encode(['message' => 'succ', 'result' => "添加成功"]));
            }
        }
        else{
            $this->db = new SysDb();
            $data = $this->db->table('tenant_advert')->where('id', $id)->item();
            $data['sort'] = $sort;
            $data['status'] = $status;
            $data['type'] = $type;
            $data['remark'] = $remark;
            $data['url'] = $url;
            $data['image'] = $imgs;
            $count = $this->db->table('tenant_advert')->where(['id' =>  $id])->update($data);
            if ($count) {
                exit(json_encode(['message' => 'succ', 'result' => "修改成功"]));
            }
        }
    }

    public function   friendlist(){
    $status = trim(input('param.status'));
    
        $where=array(
            "status" => $status,
           
        );
        foreach ($where as $key=>$value)
        {
            if(empty($value)){
                unset($where[$key]);
            }
        }
       
        $page = trim(input('param.pageNo'));
        $limit = trim(input('param.pageSize'));
        $tenantid = trim(input('param.tenantid'));
        $tol = (intval($page) - 1) * $limit;
        $list = DB::name("tenant_links")->order("create_time desc")->where($where)->where(['tenant_id' =>  $tenantid])->limit($tol, $limit)->select();
        $this->db = new SysDb();
        $count = $this->db->table('tenant_links')->where($where)->where(['tenant_id' =>  $tenantid])->count();
        $data = array();
        array_push($data, $list);
        array_push($data, $count);
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $data]));
    }

    public function frienddel()
    {
        $id = trim(input('param.id'));
        $admin = Db::name('tenant_links')->where('id', $id)->delete();;
        if ($admin) {
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "删除成功"]));
        }
    }

    public function friendupadteadd()
    {
        $id = trim(input('param.id'));
        $sort = trim(input('param.sort'));
        $status = trim(input('param.status'));
        $name = trim(input('param.name'));
        $remark = trim(input('param.remark'));
        $url = trim(input('param.url'));
        if ($id == "kk") {
            $data['sort'] = $sort;
            $data['status'] = $status;
            $data['name'] = $name;
            $data['remark'] = $remark;
            $data['url'] = $url;
            $data['tenant_id'] =trim(input('param.tenantid'));
            $this->db = new SysDb();

            $count = $this->db->table('tenant_links')->insert($data);
            if ($count) {
                exit(json_encode(['message' => 'succ', 'result' => "添加成功"]));
            }
        }
        else{
            $this->db = new SysDb();
            $data = $this->db->table('tenant_links')->where('id', $id)->item();
            $data['sort'] = $sort;
            $data['status'] = $status;
            $data['name'] = $name;
            $data['remark'] = $remark;
            $data['url'] = $url;
          
            $count = $this->db->table('tenant_links')->where(['id' =>  $id])->update($data);
            if ($count) {
                exit(json_encode(['message' => 'succ', 'result' => "修改成功"]));
            }
        }
    }
}
